﻿@using OA.Models

@(
    Html.Kendo().Grid<Account>()
    .Name("grid")
    .HtmlAttributes(new { style = "height:99%" })
    .Filterable()
    .Pageable()
    .Selectable()
    .Scrollable()
    .ToolBar(
        toolbar => {
            toolbar.Template(
                @<button class='k-button k-button-icontext k-grid-add' onclick="openInsertWindow()">
                    <span class="k-icon k-i-add"></span>新增
                </button>
            );
        }
    )
    .Columns(
        columns =>
        {
            columns.Bound<string>(c => c.Id);
            columns.Bound(c => c.Name).Width(130);
            columns.Bound(u => u.AccountState)
            .ClientTemplate(
                "# if(AccountState == 0) { #" +
                    "申请中" +
                "# } else if (AccountState == 1) { # " +
                    "" +
                "# } else { #" +
                    "锁定" +
                "# } #"
            ).Width(120);
            columns.Bound(c => c.Createdate).Format("{0:yyyy年MM月dd日}").Width(150);
            columns.Bound(c => c.Gender).Width(90);
            columns.Bound(c => c.Phone).Width(150);
            columns.Bound(c => c.State).Width(130);
            columns.Bound(c => c.Post.Name).Width(120);
            columns.Bound(c => c.Department.Name).Width(120);
            columns.Command(c =>
            {
                c.Custom("edit").Text("编辑").Click("openEditWindow");
                c.Custom("freeze").Text("解锁").Click("freezeAccount");
                c.Custom("reset").Text("密码重置").Click("resetPasssword");
                c.Custom("delete").Text("删除").Click("deleteAccount");
            }).Width(330);
        }
    )
    .DataSource(
        dataSource => dataSource
            .Ajax()
            .ServerOperation(true)
            .PageSize(18)
            .Read(read => read.Action("Read", "Accounts"))
            .Events(events => events.Error("dataSourceError"))
    )
    .Events(e => e.DataBound("dataBound"))
)

<script>
    /*
    * @@数据绑定事件
    */
    function dataBound() {
        var grid = this;
        /*
        * 如果账户冻结状态, 则显示开始按钮
        * 否则显示冻结按钮
        */
        grid.tbody.find('>tr').each(function () {
            var dataItem = grid.dataItem(this);
            var text = "";
            if (dataItem.AccountState == "0") {
                text = "启用";
            }
            else if (dataItem.AccountState == "1") {
                text = "锁定";
            }
            else {
                text = "解锁";
            }
            $(this).find(".k-grid-freeze").html(text);
        })
    }

    /*
    * @@打开新建账户窗口
    */
    function openInsertWindow() {
        openWindow('新建账户', '/Accounts/Update', { Id: 0 });
    }

    /*
    * @@打开编辑账户窗口
    */
    function openEditWindow(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var data = { Id: dataItem.Id };
        openWindow('编辑账户', '/Accounts/Update', data)
    }

    /**
    * @@ 删除账户
    */
    function deleteAccount(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        kendo.confirm("你确定要删除该账户吗?", "删除账户").then(function () {
            $.post({
                url: "/Accounts/Delete",
                dataType: 'json',
                data: { Id: dataItem.Id },
                success: function (response) {
                    $("#grid").data("kendoGrid").dataSource.read();
                    openNotification(response.Message, "success");
                },
                error: ajaxError
            })
        })
    }

    /*
    * 冻结或者开启账户
    */
    function freezeAccount(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        $.post({
            url: "/Accounts/LockOrUnlockAccount",
            dataType: 'json',
            data: { Id: dataItem.Id },
            success: function (response) {
                $("#grid").data("kendoGrid").dataSource.read();
                openNotification(response.Message, "success");
            },
            error: ajaxError
        })
    }

    /*
    * 重置密码
    */
    function resetPasssword(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        kendo.confirm("你确定要为该账户重置密码吗?", "重置密码").then(function () {
            $.post({
                url: "/Accounts/ResetPassword",
                dataType: 'json',
                data: { Id: dataItem.Id },
                success: function (response) {
                    $("#grid").data("kendoGrid").dataSource.read();
                    openNotification(response.Message, "success");
                },
                error: ajaxError
            })
        })
    }
</script>